const express = require('express')
const router = express.Router()
const Category = require('../models').Category
const config = require('../config')
const mongoose = require('mongoose')
const ObjectId = mongoose.Types.ObjectId

router.get('/categories/top', (req, res, next) => {
  Category.find({
    pid: 0
  }).then(docs => {
    res.json({
      success: true,
      ret: {
        categories: docs
      }
    })
  }).catch(err => {
    next(err)
  })
})

router.get('/categories/top/:topid', (req, res, next) => {
  Category.find({
    // 关于这里的 ID，手动通过 mongoose.Types.ObjectId 来构造（传入一个字符串id）
    pid: new ObjectId(req.params.topid)
  }).then(docs => {
    res.json({
      success: true,
      ret: {
        categories: docs
      }
    })
  }).catch(err => {
    next(err)
  })
})

module.exports = router
